<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\ProfitSharingReceiver;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Illuminate\Support\Facades\Log;
use App\Contracts\WxProfitSharing;

class ProfitSharingReceiverController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ProfitSharingReceiver(), function (Grid $grid) {
            $grid->id->sortable();
            $grid->sub_appid;
            $grid->sub_mchid;
            $grid->pay_notify_url;
            $grid->type;
            $grid->account;
            $grid->name;
            $grid->relation_type;
            $grid->amount;
            $grid->desc;
            $grid->created_at;
            $grid->updated_at->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('name');
                $filter->like('account');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ProfitSharingReceiver(), function (Show $show) {
            $show->id;
            $show->sub_appid;
            $show->sub_mchid;
            $show->pay_notify_url;
            $show->type;
            $show->account;
            $show->name;
            $show->relation_type;
            $show->amount;
            $show->desc;
            $show->created_at;
            $show->updated_at;
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ProfitSharingReceiver(), function (Form $form) {

            $form->submitted(function (Form $form){
                $receiver = [
                    'type'=>$form->type,
                    'account'=>$form->account,
                    'name'=>$form->name,
                    'relation_type'=>$form->relation_type
                ];
                $wxProfitShare = new WxProfitSharing(config('WxPs'));
                $res = $wxProfitShare->addReceiver($form->sub_appid, $form->sub_mchid, $receiver);
                if($res['return_code'] != 'SUCCESS' or $res['result_code'] != 'SUCCESS'){
                    return $form->error($res['err_code'].':'.$res['err_code_des']);
                }

            });

            $form->saving(function (Form $form){
                $receiver = [
                    'type'=>$form->type,
                    'account'=>$form->account,
                    'name'=>$form->name,
                    'relation_type'=>$form->relation_type
                ];
                $wxProfitShare = new WxProfitSharing(config('WxPs'));
                $res = $wxProfitShare->addReceiver($form->sub_appid, $form->sub_mchid, $receiver);
                if($res['return_code'] != 'SUCCESS' or $res['result_code'] != 'SUCCESS'){
                    return $form->error($res['err_code'].':'.$res['err_code_des']);
                }

            });

            $form->deleting(function (Form $form){
                $data = $form->model()->toArray();
                $receiver = [
                    'type'=>$data[0]['type'],
                    'account'=>$data[0]['account'],
                    'name'=>$data[0]['name'],
                    'relation_type'=>$data[0]['relation_type']
                ];
                $wxProfitShare = new WxProfitSharing(config('WxPs'));
                $res = $wxProfitShare->removeReceiver($data[0]['sub_appid'], $data[0]['sub_mchid'], $receiver);
                if($res['return_code'] != 'SUCCESS' or $res['result_code'] != 'SUCCESS'){
                    return $form->error($res['err_code'].':'.$res['err_code_des']);
                }

            });

            $type = [
                'MERCHANT_ID'=>'商户ID',
                'PERSONAL_WECHATID'=>'个人微信号',
            ];

            $relation_type = [
                'SERVICE_PROVIDER'=>'服务商',
                'STORE'=>'门店',
                'PARTNER'=>'合作伙伴',
            ];

            $form->display('id');
            $form->text('sub_appid');
            $form->text('sub_mchid');
            $form->text('pay_notify_url');
            $form->select('type')->options($type);
            $form->text('account');
            $form->text('name');
            $form->select('relation_type')->options($relation_type);
            $form->number('amount');
            $form->text('desc');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
